<?php
class communicationsActions extends baseDisplayActions{
  
  function getClassName($request){  	
  	return 'communication';
  }
  
   public function executeRenderList(sfWebRequest $request)
  {
  	$user = sfContext::getInstance()->getUser()->getUserProfile();
	$this->hide = array();
	$this->add = array();	
    $className = $this->getClassName($request);
    
  	$this->orderByCol = $request->getParameter('order');
  	$page = (int)$request->getParameter('page',1);
    $this->view = $request->getParameter('view', 'mesmessages');
    if(empty($this->view)) $this->view = 'mesmessages';
	$this->objDef = new objectDef($className);
	$descCol = $this->objDef->descriptor;
	if(empty($this->orderByCol)){
  		if($descCol!=''){
  			$this->orderByCol =  $this->objDef->orderFields;
  			if($this->orderByCol=='') {
  				$this->orderByCol = $descCol.'*asc';	
  			}
  		}
  	}

  	$orderSens = 'asc';
	$orderArray = array();
    if(!empty($this->orderByCol)){
    	$arrayTmp = explode(';',$this->orderByCol);
    	foreach($arrayTmp as $orderString){
    		$orderColArray=explode('*',$orderString);
    		if(count($orderColArray)>0){
    			if(isset($orderColArray[1]))
    				$orderSens = $orderColArray[1];
    		}
    		$orderArray[] = $orderColArray;
    	}
    	
    }else{
    	$orderColArray=array();
    }
  	$orderArray[] = array('id',$orderSens);  	
  	$this->q = $this->objDef->getBaseQuery($this->hide, $this->add, $orderArray);
    if($this->view=='destinedtome'){
	}elseif($this->view=='all'){
		$this->objDef->isReadOnly = true;
		$this->objDef->canBeDeleted = false;
	}else{
  		$this->q->addWhere('a.created_by_id = ?', $user->id);
	}
  	$search = new searchBar();	
	$search->addFilterstoQuery($this->q);
	$this->q->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);
    $this->nbPosts = $request->getParameter('nbPosts');
	if(!isset($this->nbPosts) or $this->nbPosts == 0){
		$this->nbPosts = sfConfig::get('app_posts_number_per_page', 10);
	}
	$this->pager = $search->setPager($request, $this->q, $this->nbPosts, $page);
	$this->totalRecords = $this->pager->getNbResults();
		
	$this->records = $this->pager->getResults(); //$q->execute();
	
	$this->addButton = !$this->objDef->isReadOnly;
	$this->orderParams = '1'; /*So it will go back to first page when users click to order by*/
	$this->module = $this->getModuleName();
	$this->moduleRef = 'messages';
	
  	return $this->renderPartial('list'); 
	
  }
  
  public function executeIndex(sfWebRequest $request){
  	$this->view = $request->getParameter('view');
  	parent::executeIndex($request);
  	if($this->getUser()->getAttribute('current_app')=='touch'){
		$this->setLayout('layoutTouch');
		$response = $this->getResponse();
  		$response->addStyleSheet('default/touchScreen.css');
  		$response->removeStyleSheet('default/layout_setup.css?v'.sfConfig::get('app_version_numero'));
	}; 
  	
  }
  
  public function executeShow(sfWebRequest $request){
  	parent::executeShow($request);
  	if($this->self->public){
  		$this->hide = array('roles', 'destinataires');
  	}else{
  		$this->hide = array('datedebut', 'datefin');
  	}
  	/*Get all this parameters in order to go back to the same view I was.*/
 	$this->view = $request->getParameter('view');
	$this->objDef = new objectDef('message');
	$this->actualPage = $request->getParameter('page',1);
	$this->order = $request->getParameter('order', 'id*desc');
  	
  	if($this->getUser()->getAttribute('current_app')=='touch'){
		$this->setLayout('layoutTouch');
		$response = $this->getResponse();
  		$response->addStyleSheet('default/touchScreen.css');
  		$response->removeStyleSheet('default/layout_setup.css?v'.sfConfig::get('app_version_numero'));
	}; 
	  	
  }
  
}